﻿using MinecraftClient;
using MinecraftClient.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MinecraftClient.Commands;

namespace MinecraftClient.ChatBots.MyBots
{
    public class DashBoardBot : ChatBot
    {
        public override void Initialize()
        {
            PerformInternalCommand("move on");
            LogToConsole("DashboardBot loaded.");
            RegisterChatBotCommand("dsb", "Use Dashboard bot to unload other bots.", 
                "unload", CommandHandler);
        }

        public override void GetText(string text)
        {
            text = GetVerbatim(text);
            Regex lst = new Regex(@"^\[.+\]<Gare_TH> d lst$");
            Regex run = new Regex(@"^\[.+\]<Gare_TH> d load (\d)$");
            Regex loaded = new Regex(@"^\[.+\]<Gare_TH> d loaded$");
            Regex unload = new Regex(@"^\[.+\]<Gare_TH> d unload (\d)$");

            if (lst.IsMatch(text))
            {
                PrintBotList();
                return;
            }

            if (run.IsMatch(text))
            {
                int choice = int.Parse(run.Match(text).Groups[1].Value);
                LoadBranch(choice);
                return;
            }

            if (loaded.IsMatch(text))
            {
                SendText(GetLoadedBots());
                return;
            }

            if (unload.IsMatch(text))
            {
                int id;
                if (int.TryParse(unload.Match(text).Groups[1].Value, out id))
                {
                    UnloadScriptBot(id);
                }
            }

        }
        
        private void PrintBotList()
        {
            SendText("Script List:");
            SendText("1. MinerBot 2. AutoTree 3. GoldCraft 4. AutoDropTimed");
        }

        private void LoadBranch(int choice)
        {
            switch (choice)
            {
                case 1:
                    LoadBotScript("MinerBot", "MinerBot.cs");
                    return;
                case 2:
                    LoadBotScript("AutoTree", "AutoTree.cs");
                    return;
                case 3:
                    LoadBotScript("GoldCraft", "GoldCraft.cs");
                    LoadBotScript("AutoDropTimed", "AutoDropTimed.cs");
                    return;
                case 4:
                    LoadBotScript("AutoDropTimed", "AutoDropTimed.cs");
                    return;
            }
        }

        private void LoadBotScript(string botClassName, string filename)
        {
            if (!IsRunning(botClassName))
            {
                RunScript(filename);
                SendText(botClassName + " loaded.");
            }
            else
            {
                SendText(botClassName + " already in the queue.");
            }
        }

        private bool IsRunning(string bot)
        {
            List<ChatBot> allBots = GetLoadedChatBots();

            foreach (var b in allBots)
            {
                if (bot.Equals(b.GetType().Name)) return true;
            }
            return false;
        }

        private string GetLoadedBots()
        {
            StringBuilder result = new StringBuilder();
            foreach (var b in GetLoadedChatBots())
            {
                string name = b.GetType().Name;
                result.Append(name).Append(", ");
            }
            return result.ToString();
        }

        private void UnloadScriptBot(int id)
        {
            string BotType = "";
            switch (id)
            {
                case 1:
                    UnloadOneBot("MinerBot");
                    break;
                case 2:
                    UnloadOneBot("AutoTree");
                    break;
                case 3:
                    UnloadOneBot("GoldCraft");
                    UnloadOneBot("AutoDropTimed");
                    break;
                case 4:
                    UnloadOneBot("AutoDropTimed");
                    break;
                default:
                    SendText("Invalid choice.");
                    return;
            }
        }

        private void UnloadOneBot(string BotType)
        {
            var BotList = GetLoadedChatBots();
            List<ChatBot> ToBeRemoved = new List<ChatBot>();

            foreach (var bot in BotList)
            {
                if (bot.GetType().Name == BotType) ToBeRemoved.Add(bot);
            }

            foreach (var rBot in ToBeRemoved)
            {
                if (BotList.Contains(rBot)) BotList.Remove(rBot);
            }

            SendText("Unloaded " + BotType);
        }

        private string CommandHandler(string cmd, string[] args)
        {
            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                {
                    case "unload":
                        int id;
                        if (int.TryParse(args[1], out id))
                        {
                            UnloadScriptBot(id);
                        }
                        return "";
                }
            }

            return "";
        }



    }
}